package com.istock.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.base.export.annotation.Exporter;
import com.istock.base.serializer.JsonSerializer;
import com.istock.base.serializer.json.JsonSerializerFactory;
import com.istock.union.user.client.SSOClientUtils;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.OperLogInfo;
import com.istock.union.user.model.OperLogQuery;
import com.istock.union.user.model.SSOUser;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.service.DeptInfoService;
import com.istock.union.user.service.OperLogInfoService;

@Controller
@RequestMapping("/s/operLogInfo")
public class OperLogController {

	private static final Logger logger = LoggerFactory.getLogger(OperLogController.class);
	
	@Autowired
	private OperLogInfoService operLogService;
	@Autowired
	private DeptInfoService deptInfoService;
	
	@RequestMapping("showList")
	public String showList(ModelMap model) {
		SSOUser user = SSOClientUtils.getInstance().findCurrentUser();
		List<Map<String , Object>> deptList = deptInfoService.queryDeptTreeData(user.getTenantNo(), user.getCurrentDept() != null ?user.getCurrentDept().getDeptCode():null 
				, SSOClientUtils.getInstance().isAdmin() , false);
		JsonSerializer serializer = JsonSerializerFactory.findSerializer();
		model.put("treeData", serializer.serialize(deptList));
//		model.put("treeData", JSON.toJSONString(deptList));
		return "user-app/operLog/list";
	}
	
	
	@RequestMapping("search")
	@SecurityChecker("user:operLog:search")
	@Exporter(code="OpLog")
	public @ResponseBody ApiBaseResponse<PageInfo<OperLogInfo>> search(ModelMap model , OperLogQuery query , PageInfo<OperLogInfo> pageInfo){
		if(pageInfo == null){
			pageInfo = new PageInfo<OperLogInfo>();
		}
		if(!SSOClientUtils.getInstance().isAdmin() && query.getDeptCodeList().isEmpty()) {
			SSOUser userInfo = SSOClientUtils.getInstance().findCurrentUser();
			if(userInfo.getCurrentDept() != null) {
				query.getDeptCodeList().add(userInfo.getCurrentDept().getDeptCode());
			}
		}
		operLogService.search(query, pageInfo);
		
		return new ApiBaseResponse<PageInfo<OperLogInfo>>( RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,
				Constants.API_RESPONSE_SUCCESS_MESSAGE, pageInfo);
	}
	
	/**
	 * 删除操作日志记录
	 */
	@SecurityChecker("user:operLog:remove")
	@RequestMapping( "/remove")
	@ResponseBody
	public ApiBaseResponse<Boolean> remove(String ids) {
		int num = 0;
		try {
			List<String> idList = Arrays.asList(ids.split(","));
			num = operLogService.deleteOperLogByIds(Arrays.asList(ids.split(",")));
			if (null != idList && num == idList.size()) {
				return new ApiBaseResponse<Boolean>( RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,
						Constants.API_RESPONSE_SUCCESS_MESSAGE, true);
			}
		} catch (Exception e) {
			logger.error("Delete config info by id exception ", e);
		}
		return new ApiBaseResponse<Boolean>( RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE,
				Constants.API_RESPONSE_FAIL_MESSAGE, false);
	}
	
	@SecurityChecker("user:operLog:detail")
	@RequestMapping("/detail")
    public String detail(ModelMap model, String id) {
		model.put("operLog", operLogService.selectOperLogById(id));
        return "user-app/operLog/detail";
    }
    
    @SecurityChecker("user:operLog:clear")
    @RequestMapping("/clean")
    @ResponseBody
    public ApiBaseResponse<Boolean> clean() {
        operLogService.cleanOperLog();
        return new ApiBaseResponse<Boolean>( RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,
				Constants.API_RESPONSE_SUCCESS_MESSAGE, true);
    }
    
    @SecurityChecker("user:operLog:detail")
	@RequestMapping("/qryDetail")
	@ResponseBody
	public ApiBaseResponse<Map> qryDetail(String id) {
		if (StringUtils.isBlank(id)) {
			return new ApiBaseResponse<Map>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE, 
					"OperLogInfo id is null" , null);
		}
		Map info = operLogService.selectOperLogById(id);
		return new ApiBaseResponse<Map>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE, 
				Constants.API_RESPONSE_SUCCESS_MESSAGE, info);
	}
    
}
